package com.note.feng.dataStructureAlgorithm.array;

public class Array<E> {
    private E [] data;
    private int size;

    public Array(int capactiy){
        this.data = (E [])new Object[capactiy];
    }
    public Array(){
        this(10);
    }

    public int getCapactiy(){
        return data.length;
    }
    public int getSize(){
        return size;
    }
    public boolean isEmpty(){
        return size == 0;
    }
    public void add(int index, E e){
        if(size == data.length){
            resize(getCapactiy() * 2);
        }
        if(index < 0 || index > size){
            throw new RuntimeException("failed, index can not < 0 or > size");
        }
        //移位：若index小于size，表示在array的中间插入值，index后面的值需要后移
        for(int i = size - 1; i >= index; i--){
            data[i + 1] = data[i];
        }
        data[index] = e;
        size ++;
    }
    private void resize(int capactiy){
        E [] newData = (E [])new Object[capactiy];
        for(int i = 0; i < size; i++){
            newData[i] = data[i];
        }
        this.data = newData;
    }
    public void addFrist(E e){
        add(0, e);
    }
    public void addLast(E e){
        add(size, e);
    }

    public E get(int index){
        if(index < 0 || index > size){
            throw new RuntimeException("get failed, index can not < 0 or > size");
        }
        return data[index];
    }
    public void set(int index, E e){
        if(index < 0 || index > size){
            throw new RuntimeException("set failed, index can not < 0 or > size");
        }
        data[index] = e;
    }
    public int find(E e){
        for(int i = 0; i < size; i++){
            if(data[i].equals(e)){
                return i;
            }
        }
        return -1;
    }
    public boolean contains(E e){
        return find(e) > -1;
    }
    public E remove(int index){
        if(index < 0 || index > size){
            throw new RuntimeException("failed, index can not < 0 or > size");
        }
        E res = data[index];
        for(int i = index; i < size - 1; i ++){
            data[i] = data[i + 1];
        }
        size --;
        //缩容时，要注意边界条件
        if(size == getCapactiy() / 4 && getCapactiy() / 2 != 0){
            resize(getCapactiy() / 2);
        }
        return res;
    }
    public E removeFrist(){
        return remove(0);
    }
    public E removeLast(){
        return remove(size - 1);
    }
    public void removeElement(E e){
        for (int i = 0;i < size; i++){
            if(data[i].equals(e)){
                remove(i);
                break;
            }
        }
    }
    public E getLast() {
        return get(size - 1);
    }

    public E getFrist() {
        return get(0);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for(int i = 0; i < size; i ++){
            if(i == size - 1){
                sb.append(data[i]).append("]");
            }else{
                sb .append(data[i]+",");
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        Array<Integer> a = new Array<>(10);
        for (int i = 0 ;i < 10; i ++) {
            a.add(i, i);
        }
        a.addLast(11);
        System.out.println(a);
        a.remove(1);
        System.out.println(a);
        a.remove(1);
        System.out.println(a);
        a.remove(1);
        System.out.println(a);
        a.remove(1);
        a.remove(1);
        a.remove(1);
        a.removeElement(8);
        System.out.println(a);
        System.out.println(a.getSize());
        System.out.println(a.getCapactiy());
        int b = 0X7fffffff;
        System.out.println(b);
        System.out.println(b + b);

    }

}
